import { Injectable } from '@nestjs/common';
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { CollectionEntity } from "./collection.entity";
import { ResultVo } from "../../vo/common/result.vo";

@Injectable()
export class CollectionService {
    constructor(
        @InjectRepository(CollectionEntity)
        private readonly collectionRepository: Repository<CollectionEntity>,
    ) { }

    async queryAll(userId: number) {
        let data = await this.collectionRepository.find({ userId })
        return ResultVo.successWithData(data)
    }

    async queryOne(id: number | string) {
        let data = await this.collectionRepository.findOne(id)
        return ResultVo.successWithData(data)
    }

    async add(data: CollectionEntity) {
        let date = new Date()
        data.createdTime = date
        data.updatedTime = date
        let res = await this.collectionRepository.save<CollectionEntity>(data)
        return ResultVo.successWithData(res)
    }

    async edit(data: CollectionEntity) {
        data.updatedTime = new Date()
        let res = await this.collectionRepository.update({ id: data.id }, data)
        return ResultVo.successWithData(res)
    }

    async delete(id: number | string) {
        let res = await this.collectionRepository.delete(id)
        return ResultVo.successWithData(res)
    }
}
